package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.util.StrTools;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.ProcessingOrderChargesAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.lnfinityGradeCostAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@AttrHandler(ProcessingOrderChargesAttrHandler.class)
public class ProcessingOrderChargesAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    OperateInstAttrService operateInstAttrService;
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        KiteAttr kiteAttr12 = inst.getAttr("200003712");
        if(!KiteObjectUtils.isEmpty(kiteAttr12)){
            String discountType = kiteAttr12.getValue();
            if(KiteStringUtils.equals("100006437",discountType)){
                return;
            }
        }
        KiteProdInst prodInst = querySceneInstService.getProdInstByFuncProdInstId(data.getCustId(),inst.getInstId());
        if(KiteObjectUtils.isEmpty(prodInst)){
            return;
        }
        List<KiteProdInstFunc> funcs = querySceneInstService.listProdInstFuncsBySceneInstId(prodInst.getProdInstId(),data.getSceneInstId());
        String fieldName = attr.getFieldName();
        String acctFee = "";
        String communicationPacketType = "";
        if (KiteListUtils.isNotEmpty(funcs)) {
            for (KiteProdInstFunc func : funcs) {
                //通信套包类型
                KiteAttr kiteAttr = func.getAttr("200003697");
                if (Objects.isNull(kiteAttr)) {
                    // 资费套包类型
                    kiteAttr = func.getAttr("200004179");
                }
                if (!KiteObjectUtils.isEmpty(kiteAttr)) {
                    communicationPacketType = kiteAttr.getValue();
                }
                if(!KiteStringUtils.equals(inst.getInstId(),func.getInstId())){
                    KiteAttr acctFeeAttr = func.getAttr("200004183");
                    if(!KiteObjectUtils.isEmpty(acctFeeAttr)){
                        if(KiteStringUtils.isEmpty(acctFee)){
                            acctFee = acctFeeAttr.getValue();
                        }else{
                            acctFee = StrTools.numAdd(acctFee,acctFeeAttr.getValue());
                        }
                    }
                }
            }
        }
        String value = attr.getValue();
        //通信套包类型 月付固定共享、预付固定共享
        if (!KiteStringUtils.isEmpty(value)) {
            if (!(KiteStringUtils.equals("100007469", communicationPacketType) || KiteStringUtils.equals("100007470", communicationPacketType))) {
                //最终订购费联动
                if (KiteStringUtils.isEqual("200003717", attr.getFieldName())) {
                    KiteAttr kiteAttr18 = inst.getAttr("200003718");
                    String value18 = kiteAttr18.getValue();
                    if (!KiteStringUtils.isEmpty(value18)) {
                        String value19 = StrTools.numMultiply(value, value18);
                        value19 = StrTools.numDivide(value19, "100");
                        value19 = StrTools.numIntRoundUp(value19, 2);
                        operateInstAttrService.setAttrValue(inst, "200003719", value19, data.getSceneInstId(), null);
                        KiteAttr kiteAttr20 = inst.getAttr("200003720");
                        String value20 = kiteAttr20.getValue();
                        if (!KiteStringUtils.isEmpty(value20)) {
                            String value21 = StrTools.numMultiply(value19, value20);
                            value21 = StrTools.numDivide(value21, "100");
                            value21 = StrTools.numIntRoundUp(value21, 2);
                            operateInstAttrService.setAttrValue(inst, "200003721", value21, data.getSceneInstId(), null);
                            KiteAttr kiteAttr22 = inst.getAttr("200003722");
                            String value22 = kiteAttr22.getValue();
                            if (!KiteStringUtils.isEmpty(value22)) {
                                String value23 = StrTools.numMultiply(value21, value22);
                                value23 = StrTools.numDivide(value23, "100");
                                value23 = StrTools.numIntRoundUp(value23, 2);
                                operateInstAttrService.setAttrValue(inst, "200003723", value23, data.getSceneInstId(), null);
                            }
                        }
                    }
                }
                if (KiteStringUtils.isEqual("200003718", attr.getFieldName())) {
                    KiteAttr kiteAttr17 = inst.getAttr("200003717");
                    String value17 = kiteAttr17.getValue();
                    if (!KiteStringUtils.isEmpty(value17)) {
                        String value19 = StrTools.numMultiply(value, value17);
                        value19 = StrTools.numDivide(value19, "100");
                        value19 = StrTools.numIntRoundUp(value19, 2);
                        operateInstAttrService.setAttrValue(inst, "200003719", value19, data.getSceneInstId(), null);
                        KiteAttr kiteAttr20 = inst.getAttr("200003720");
                        String value20 = kiteAttr20.getValue();
                        if (!KiteStringUtils.isEmpty(value20)) {
                            String value21 = StrTools.numMultiply(value19, value20);
                            value21 = StrTools.numDivide(value21, "100");
                            value21 = StrTools.numIntRoundUp(value21, 2);
                            operateInstAttrService.setAttrValue(inst, "200003721", value21, data.getSceneInstId(), null);
                            KiteAttr kiteAttr22 = inst.getAttr("200003722");
                            String value22 = kiteAttr22.getValue();
                            if (!KiteStringUtils.isEmpty(value22)) {
                                String value23 = StrTools.numMultiply(value21, value22);
                                value23 = StrTools.numDivide(value23, "100");
                                value23 = StrTools.numIntRoundUp(value23, 2);
                                operateInstAttrService.setAttrValue(inst, "200003723", value23, data.getSceneInstId(), null);
                            }
                        }
                    }
                }

                if (KiteStringUtils.isEqual("200003720", attr.getFieldName())) {
                    KiteAttr kiteAttr19 = inst.getAttr("200003719");
                    String value19 = kiteAttr19.getValue();
                    if (!KiteStringUtils.isEmpty(value19)) {
                        String value21 = StrTools.numMultiply(value, value19);
                        value21 = StrTools.numDivide(value21, "100");
                        value21 = StrTools.numIntRoundUp(value21, 2);
                        operateInstAttrService.setAttrValue(inst, "200003721", value21, data.getSceneInstId(), null);
                        KiteAttr kiteAttr22 = inst.getAttr("200003722");
                        String value22 = kiteAttr22.getValue();
                        if (!KiteStringUtils.isEmpty(value22)) {
                            String value23 = StrTools.numMultiply(value21, value22);
                            value23 = StrTools.numDivide(value23, "100");
                            value23 = StrTools.numIntRoundUp(value23, 2);
                            operateInstAttrService.setAttrValue(inst, "200003723", value23, data.getSceneInstId(), null);
                        }
                    }
                }
                if (KiteStringUtils.isEqual("200003722", attr.getFieldName())) {
                    KiteAttr kiteAttr21 = inst.getAttr("200003721");
                    String value21 = kiteAttr21.getValue();
                    if (!KiteStringUtils.isEmpty(value21)) {
                        String value23 = StrTools.numMultiply(value, value21);
                        value23 = StrTools.numDivide(value23, "100");
                        value23 = StrTools.numIntRoundUp(value23, 2);
                        operateInstAttrService.setAttrValue(inst, "200003723", value23, data.getSceneInstId(), null);
                    }
                }
                SpringUtil.getOrRegisterBean(lnfinityGradeCostAttrHandler.class).calculate(attr, (KiteProdInstFunc) inst, value, data);
            }
            else {
                //最终账户费联动
                if (KiteStringUtils.isEqual("200004185", attr.getFieldName())) {
                    KiteAttr kiteAttr18 = inst.getAttr("200003718");
                    String value18 = kiteAttr18.getValue();
                    if (!KiteStringUtils.isEmpty(value18)) {
                        String value19 = StrTools.numMultiply(value, value18);
                        value19 = StrTools.numDivide(value19, "100");
                        value19 = StrTools.numIntRoundUp(value19, 2);
                        operateInstAttrService.setAttrValue(inst, "200004186", value19, data.getSceneInstId(), null);
                        KiteAttr kiteAttr20 = inst.getAttr("200003720");
                        String value20 = kiteAttr20.getValue();
                        if (!KiteStringUtils.isEmpty(value20)) {
                            String value21 = StrTools.numMultiply(value19, value20);
                            value21 = StrTools.numDivide(value21, "100");
                            value21 = StrTools.numIntRoundUp(value21, 2);
                            operateInstAttrService.setAttrValue(inst, "200004187", value21, data.getSceneInstId(), null);
                            KiteAttr kiteAttr22 = inst.getAttr("200003722");
                            String value22 = kiteAttr22.getValue();
                            if (!KiteStringUtils.isEmpty(value22)) {
                                String value23 = StrTools.numMultiply(value21, value22);
                                value23 = StrTools.numDivide(value23, "100");
                                value23 = StrTools.numIntRoundUp(value23, 2);
                                operateInstAttrService.setAttrValue(inst, "200004188", value23, data.getSceneInstId(), null);
                            }
                        }
                    }
                }
                if (KiteStringUtils.isEqual("200003718", attr.getFieldName())) {
                    KiteAttr kiteAttr85 = inst.getAttr("200004185");
                    String value17 = kiteAttr85.getValue();
                    if (!KiteStringUtils.isEmpty(value17)) {
                        String value19 = StrTools.numMultiply(value, value17);
                        value19 = StrTools.numDivide(value19, "100");
                        value19 = StrTools.numIntRoundUp(value19, 2);
                        operateInstAttrService.setAttrValue(inst, "200004186", value19, data.getSceneInstId(), null);
                        KiteAttr kiteAttr20 = inst.getAttr("200003720");
                        String value20 = kiteAttr20.getValue();
                        if (!KiteStringUtils.isEmpty(value20)) {
                            String value21 = StrTools.numMultiply(value19, value20);
                            value21 = StrTools.numDivide(value21, "100");
                            value21 = StrTools.numIntRoundUp(value21, 2);
                            operateInstAttrService.setAttrValue(inst, "200004187", value21, data.getSceneInstId(), null);
                            KiteAttr kiteAttr22 = inst.getAttr("200003722");
                            String value22 = kiteAttr22.getValue();
                            if (!KiteStringUtils.isEmpty(value22)) {
                                String value23 = StrTools.numMultiply(value21, value22);
                                value23 = StrTools.numDivide(value23, "100");
                                value23 = StrTools.numIntRoundUp(value23, 2);
                                operateInstAttrService.setAttrValue(inst, "200004188", value23, data.getSceneInstId(), null);
                            }
                        }
                    }
                }

                if (KiteStringUtils.isEqual("200003720", attr.getFieldName())) {
                    KiteAttr kiteAttr86 = inst.getAttr("200004186");
                    String value19 = kiteAttr86.getValue();
                    if (!KiteStringUtils.isEmpty(value19)) {
                        String value21 = StrTools.numMultiply(value, value19);
                        value21 = StrTools.numDivide(value21, "100");
                        value21 = StrTools.numIntRoundUp(value21, 2);
                        operateInstAttrService.setAttrValue(inst, "200004187", value21, data.getSceneInstId(), null);
                        KiteAttr kiteAttr22 = inst.getAttr("200003722");
                        String value22 = kiteAttr22.getValue();
                        if (!KiteStringUtils.isEmpty(value22)) {
                            String value23 = StrTools.numMultiply(value21, value22);
                            value23 = StrTools.numDivide(value23, "100");
                            value23 = StrTools.numIntRoundUp(value23, 2);
                            operateInstAttrService.setAttrValue(inst, "200004188", value23, data.getSceneInstId(), null);
                        }
                    }
                }
                if (KiteStringUtils.isEqual("200003722", attr.getFieldName())) {
                    KiteAttr kiteAttr87 = inst.getAttr("200004187");
                    String value21 = kiteAttr87.getValue();
                    if (!KiteStringUtils.isEmpty(value21)) {
                        String value23 = StrTools.numMultiply(value, value21);
                        value23 = StrTools.numDivide(value23, "100");
                        value23 = StrTools.numIntRoundUp(value23, 2);
                        operateInstAttrService.setAttrValue(inst, "200004188", value23, data.getSceneInstId(), null);
                    }
                }
                //
                KiteAttr KiteAttr85 = inst.getAttr("200004185");
                //
                KiteAttr KiteAttr86 = inst.getAttr("200004186");
                //
                KiteAttr KiteAttr87 = inst.getAttr("200004187");
                //
                KiteAttr KiteAttr88 = inst.getAttr("200004188");
                if(!KiteStringUtils.isEmpty(KiteAttr85.getValue())&&!KiteStringUtils.isEmpty(KiteAttr86.getValue())
                    &&!KiteStringUtils.isEmpty(KiteAttr87.getValue())&&!KiteStringUtils.isEmpty(KiteAttr88.getValue())){
                    String countFee = StrTools.numAdd(KiteAttr85.getValue(),KiteAttr86.getValue());
                    countFee = StrTools.numAdd(countFee,KiteAttr87.getValue());
                    countFee = StrTools.numAdd(countFee,KiteAttr88.getValue());
                    if(!KiteStringUtils.isEmpty(acctFee)){
                        String discountRate = StrTools.numDivide(countFee, acctFee,4);
                        discountRate = StrTools.numMultiply(discountRate,"100");
                        discountRate = StrTools.numIntRoundUp(discountRate,2);
                        operateInstAttrService.setAttrValue(inst, "200001694", discountRate, data.getSceneInstId(), null);
                    }
                }
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {
        System.out.println();

    }
}
